package com.clinic.dao.imp;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.clinic.dao.interfaces.Center_dao;
import com.clinic.domain.Center;
import com.clinic.util.Util_imp;

public class Center_imp implements Center_dao {

	private SessionFactory session_factory;

	public Center_imp() {
		session_factory = Util_imp.get_sessionfactory();
	}

	@Override
	public int create_center(Center center) {
		System.out.println("Enter - create_center");
		Session session = session_factory.openSession();
		Transaction tx = null;
		int cr_obj_id = 0;
		try {
			tx = session.beginTransaction();
			cr_obj_id = (Integer) session.save(center);
			tx.commit();
			System.out.println("New Center Created with ID = " + cr_obj_id);
		} catch (HibernateException ex) {
			if (tx != null) {
				System.out.println("Now Create Center Rooled back");
				tx.rollback();
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - create_center");
		return cr_obj_id;
	}

	@Override
	public void update_center(Center ce) {
		System.out.println("Enter - update_center");
		Session session = session_factory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(ce);
			tx.commit();
			System.out.println("Center Object Updated");
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
				System.out.println("Now Update Center Rooled back");
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - update_center");
	}

	@Override
	public void delete_center(int cr_obj_id) {
		System.out.println("Enter - delete_center");
		Session session = session_factory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			Center c = (Center) session.get(Center.class, cr_obj_id);
			session.delete(c);
			tx.commit();
			System.out.println("Center Object with Object ID: " + cr_obj_id
					+ " Deleted");
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
				System.out.println("Now Delete Center Rooled back");
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - delete_center");
	}

	@Override
	public Center get_center(int cr_obj_id) {
		System.out.println("Enter - get_center");
		Session session = session_factory.openSession();
		Transaction tx = null;
		Center c = null;

		try {
			tx = session.beginTransaction();
			c = (Center) session.createQuery("from Center where cr_obj_id = :id")
					.setParameter("id", cr_obj_id).uniqueResult();
			tx.commit();
			System.out.println("Returned Object Name: " + c.getName());
		} catch (HibernateException ex) {
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - get_center");
		return c;
	}

	@Override
	public Center persistOrMerge(Center c) {
		return (Center) session_factory.getCurrentSession().merge(c);
	}
	
	@Override
	public int getsize(){
		System.out.println("Enter - getsize");
		Session session = session_factory.openSession();
		int size = 0;
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			size = ((Long) session.createQuery("select count(*) from Center").uniqueResult()).intValue();
			tx.commit();
			System.out.println("Count(*) Center: " + size);
		} catch (HibernateException ex) {
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - getsize");
		return size;
	}
}